<?php
/**
 * Geneone: Content Management Framework
 * Search Class
 *
 * http://geneone.neondragon.net/
 *
 * This file contains GPL licensed code from 
 * http://www.chuggnutt.com/stemmer-source.php
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Search {
	/**
	 * Returns search terms stemmed from search query.
	 *
	 * @var string $query Query
	 * @var boolean $group Whether to group words together
	 *
	 * @return array Search Terms
	 */
	function getTerms($query, $group=true) {
		$query = str_word_count(strtolower($query), 1); // Get all terms
		
		$stopwords = array(
			'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 
			'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 
			'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 
			'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 
			'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 
			'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 
			'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 
			'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 
			'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 
			'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 
			'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 
			'than', 'too', 'very',
		);
		$query = array_diff($query, $stopwords);
		
		if ($group) {
			$query = array_count_values($query);
			$q = array();
			
			foreach ($query as $j => $k) {
				$stemmed = Gene_Search::getStemmed($j);
				$q[$stemmed] = $k;
			}
			
			return $q;
		} else {
			foreach ($query as $j => $k) {
				$query[$k] = Gene_Search::getStemmed($k);
			}
			
			return $query;
		}
	}

	/**
	 * Returns stemmed version of a word.
	 *
	 * @var string $word Word
	 *
	 * @return string Stemmed Word
	 */
	function getStemmed($word) {
		require_once ('Gene/Search/Stem.php');
		
		static $stemmed = array();
		
		if (!isset($stemmed[$word])) {
			$stemmed[$word] = Gene_Search_Stem::stem($word);
		}
		
		return $stemmed[$word];
	}
	
	/**
	 * Returns search source.
	 *
	 * @var string $source Source
	 *
	 * @return object Gene_Search_Source object or PEAR_Error on failure
	 */
	function &getSource($source) {
		$sources = Gene::getSetting("search_sources");
		if (in_array($source, $sources)) {
			if (include_once("Gene/Search/Source/".ucfirst($source).".php")) {
				$class = "Gene_Search_Source_".ucfirst($source);
				$s =& new $class;
			} else {
				$s =& PEAR::raiseError("The specified search source is not available.");
			}
		} else {
			$s =& PEAR::raiseError("The specified search source is not available.");
		}
		
		return $s;
	}
}
 
?>